<?php

namespace Home\Model;

use Think\Model;

class TaskModel extends Model {

    protected $tableName = 'task';

    function getStore($user_id) {
        $where = array('user_id' => $user_id);
        return M('store')->where($where)->select();
    }

    function getPackage() {
        return M('package')->order('sort asc')->select();
    }

    function getStoreName($store_id) {
        return M('store')->getFieldById($store_id, 'name');
    }

    function getPackageName($package_id) {
        return M('package')->getFieldById($package_id, 'name');
    }

    function addTask($data) {
        $return = array('info' => '提交失败！', 'status' => 0, 'new_id' => 0);
        M()->startTrans(); //开启事务
        try {
            $result1 = $this->add($data);
            if (!$result1) {
                E('发布失败！');
            }
            $price = M('package')->getFieldById($data['package_id'], 'price');
            $user_info = M('user')->find($data['user_id']);
            if ($user_info['amount'] < $price) {
                E('账户余额不足！');
            }
            $amount = $user_info['amount'] - $price;
            $data2 = array('amount' => $amount);
            $result2 = M('user')->where(array('id' => $data['user_id']))->save($data2);
            if ($result2 === false) {
                E('发布失败！');
            }
            $data3 = array(
                'type' => 3,
                'user_id' => $user_info['id'],
                'user_name' => $user_info['name'],
                'star_amount' => $user_info['amount'],
                'transaction_amount' => $price,
                'amount' => $amount,
                'add_time' => time()
            );
            $result3 = M('transaction')->add($data3);
            if (!$result3) {
                E('发布失败！');
            }
            M()->commit(); //提交事务
            $return['new_id'] = $result1;
            $return['status'] = 1;
            $return['info'] = '发布成功！';
        } catch (\Exception $e) {
            M()->rollback(); //回滚
            $return['status'] = 0;
            $return['info'] = $e->getMessage();
        }
        return $return;
    }

    function modify($id, $data) {
        $where = array('id' => $id);
        return $this->where($where)->save($data);
    }

    function getList($where = array()) {
        $page_size = 10;
        $count = $this->where($where)->count();
        $page = new \Think\Page($count, $page_size);
        $list = $this->where($where)->order('add_time desc')->limit($page->firstRow, $page_size)->select();
        $p = I('get.p', 1, 'intval');
        $html = page($p, $page->totalRows, $page_size, 8);
        return array('list' => $list, 'page' => $html, 'star' => $page->firstRow);
    }

    function addChange($data) {
        return M('transaction')->add($data);
    }

    function getUserAmount($user_id) {
        return M('user')->getFieldById($user_id, 'amount');
    }

    function getCashAmount($user_id) {
        $where = array('user_id' => $user_id, 'status' => 0, 'type' => 2);
        $result = M('transaction')->where($where)->sum('transaction_amount');
        return $result ? $result : 0;
    }

    function addCash($data) {
        return M('transaction')->add($data);
    }

    function getTransactionRecords($type, $user_id) {
        $where = array('user_id' => $user_id);
        if ($type > 0) {
            $where['type'] = $type;
        }
        $page_size = 10;
        $count = M('transaction')->where($where)->count();
        $page = new \Think\Page($count, $page_size);
        $list = M('transaction')->where($where)->order('add_time desc')->limit($page->firstRow, $page_size)->select();
        $p = I('get.p', 1, 'intval');
        $html = page($p, $page->totalRows, $page_size, 8);
        return array('list' => $list, 'page' => $html, 'star' => $page->firstRow);
    }

    function getTaskInfo($id, $user_id) {
        $where = array('id' => $id, 'user_id' => $user_id);
        return $this->where($where)->find();
    }

    function getPackageInfo($id) {
        $resule=M('package')->find($id);
        return $resule['description'];
    }

}
